package yz.com.javautil.test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/*******************************************************
 @author    : songquan

 -- purpose   : 热点数据日汇总
 -- source    : tb_odb_gsmdata
 -- target    : tb_mdb_gsmdata_stat_day
 -- modified  : 李四
 -- modified_date : 2021-8-25 10:11:42
 -- reason    : 增加归属地代码字段
 *******************************************************/
public class DateUtil {
    /**
     * 获取当前时间  "yyyy-MM-dd HH:mm:ss"
     *
     * @return
     */
    public static String getNowTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        /**
         * 设置日期格式
         */

        String now = sdf.format(new Date());
        return now;
    }


    /**
     * 获取某段时间的日期数组
     *
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return List<Map < String, Object>>
     * @throws ParseException
     */
    public static List<Map<String, Object>> getDataList(String startTime, String endTime) throws ParseException {

        //获取某段时间的日期数组
        List<Map<String, Object>> bfhmThDays = new ArrayList<>();

        List<Date> dataList = DateUtil.findDates(startTime, endTime);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        for (Date datacc : dataList) {
            String dateString = formatter.format(datacc);
            Map<String, Object> dataMap = new HashMap<>(16);
            dataMap.put("thdays", dateString);
            bfhmThDays.add(dataMap);
        }


        return bfhmThDays;

    }

    /**
     * 验证日期是否合法  "yyyy-MM-dd HH:mm:ss"
     *
     * @param str
     * @return
     */
    public static boolean isValidDate(String str) {
        boolean convertSuccess = true;
        // 指定日期格式；
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007-02-29会被接受，并转换成2007-03-01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }

        return convertSuccess;
    }

    public static boolean isValidDate2(String str) {
        boolean convertSuccess = true;
        // 指定日期格式；
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        try {
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    public static boolean isValidDate3(String str) {
        boolean convertSuccess = true;
        // 指定日期格式；
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
        try {
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    public static boolean isValidDate4(String str) {
        boolean convertSuccess = true;
        // 指定日期格式；
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日HH点mm分ss秒");
        try {
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    public static boolean isValidDate5(String str) {
        boolean convertSuccess = true;
        // 指定日期格式；
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd H:mm:ss");
        try {
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    public static boolean isValidDate6(String str) {
        boolean convertSuccess = true;
        // 指定日期格式；
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd H:mm");
        try {
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    public static boolean isValidDate7(String str) {
        boolean convertSuccess = true;
        // 指定日期格式；
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:mm");
        try {
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    public static boolean isValidDate8(String str) {
        boolean convertSuccess = true;
        // 指定日期格式；
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    public static boolean isValidDate9(String str) {
        boolean convertSuccess = true;
        // 指定日期格式；
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
        try {
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    public static boolean isValidDate10(String str) {
        boolean convertSuccess = true;
        // 指定日期格式；
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        try {
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            convertSuccess = false;
        }
        return convertSuccess;
    }

    /**
     * 中文正则
     */
    private static String REGEX_CHINESE = "[\u4e00-\u9fa5]";



    /*    *//**
     * 字符串转日期
     * @param str   43306.4153240741
     * @return 日期格式 1990-03-06 00:00:00
     *//*
    public static String dataToStr(String str) throws ParseException {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date data = org.apache.poi.ss.usermodel.DateUtil.getJavaDate(Double.valueOf(str));

        String dateString = formatter.format(data);

        return dateString;
    }*/

    /**
     * 日期转换成满足格式的字符串 "yyyy-MM-dd HH:mm:ss"
     *
     * @param date
     * @return str "yyyy-MM-dd HH:mm:ss"
     */
    public static String dateSavedInDb(Date date) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String finalStrDate = sdf.format(date);
        return finalStrDate;
    }

    /**
     * 将时间转换为时间戳
     *
     * @param s
     * @return
     * @throws ParseException
     */
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/M/d H:mm");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    /**
     * 时间字符串去除“-”,例：2019-02-01 转换成20190201
     *
     * @param a
     * @return
     */
    public static String quietsigals(String[] a) {
        List list = new ArrayList();
        for (int i = 0; i < a.length; i++) {
            int n = a[i].indexOf("-");
            String b = a[i].substring(0, n);
            int m = a[i].lastIndexOf("-");
            String c = a[i].substring(n + 1, m);
            String d = a[i].substring(m + 1);
            list.add(b + c + d);

        }
        return list.toString();
    }

    /**
     * 时间字符串去除“-”,例：2019-02-01 转换成20190201
     *
     * @param a
     * @return
     */
    public static String quietsigal(String a) {
        int n = a.indexOf("-");
        String b = a.substring(0, n);
        int m = a.lastIndexOf("-");
        String c = a.substring(n + 1, m);
        String d = a.substring(m + 1);
        return b + c + d;
    }

    /**
     * 日期字符穿取前面年月日转整型
     *
     * @param a
     * @return
     */
    public static int stringtimetoint(String a) {
        int n = a.indexOf("-");
        String b = a.substring(0, n);
        int m = a.lastIndexOf("-");
        String c = a.substring(n + 1, m);
        String d = a.substring(m + 1);
        String k = b + c + d;
        return Integer.parseInt(k.trim().substring(0, 8));
    }

    /**
     * 向字符串固定位置插入/ 例：20190101 成2019/01/01
     *
     * @param a
     * @return
     */
    public static String insertsingal(int a) {
        String string = String.valueOf(a);
        StringBuilder sb = new StringBuilder(string);
        sb.insert(4, "/");
        sb.insert(7, "/");
        return sb.toString();
    }

    /* *//**
     * 比较两个时间的时间差，精确到天、时、分、秒
     * @param firstTime
     * @param nowTime
     * @return
     *//*
    public static JSONObject getDistanceTime(String firstTime, String nowTime) {
        JSONObject dataMap = new JSONObject();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1;
        try {
            d1 = df.parse(nowTime);
            *//**
     * 用户初次登录时间
     *//*


            Date d2 = df.parse(firstTime);
            long diff = d1.getTime() - d2.getTime();
            *//**
     * 当前的时间减去我初次登陆的时间如果大于等于2小时
     *//*
     *//**
     * 共计秒数
     *//*
            long seconds = diff / (1000);

            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);

            dataMap.put("hours", hours);
            dataMap.put("minutes", minutes);
            dataMap.put("seconds", seconds);
            dataMap.put("days", days);
            dataMap.put("absoluteSeconds", Math.abs(seconds));
            return dataMap;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dataMap;
    }*/

    /**
     * 比较两个时间大小
     *
     * @param beginTime
     * @param endTime
     * @return
     */
    public static String comparetoTime(String beginTime, String endTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //String beginTime = "2018-06-01";
        //String endTime = "2018-05-31";
        Date bt = sdf.parse(beginTime);
        Date et = sdf.parse(endTime);
        if (bt.before(et)) {
            //表示bt小于et

            return "true";
        } else {
            //--反之
            if (beginTime.equals(endTime)) {

                return "true";
            } else {

                return "false";
            }

        }

    }


//    public static void main(String[] args) {
//        String yearFirst = getCurrYearFirst();
//        System.out.println(yearFirst);
//        String currYearLast = getCurrYearLast();
//        System.out.println(currYearLast);
//    }

    /**
     * 获取当前年的第一天
     *
     * @return
     */
    public static String getCurrYearFirst() {
        Calendar currCal = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, currCal.get(Calendar.YEAR));
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(time);
    }

    /**
     * 获取当前年的最后一天
     *
     * @return
     */
    public static String getCurrYearLast() {
        Calendar currCal = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, currCal.get(Calendar.YEAR));
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(time);
    }

    /**
     * 按天改变日期值
     *
     * @param date    要修改的日期
     * @param daysNum 天数（正值为增加 负值为减少）
     * @return date
     */
    public static Date getDateByDay(Date date, int daysNum) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, daysNum);
        return calendar.getTime();
    }

    /**
     * 根据日期获取最小时段及最大时段日期
     *
     * @param date 2021-11-01
     * @param type MIN MAX
     * @return 2021-11-01 00:00:00 或 2021-11-01 23:59:59
     */
    public static String getStrDateByType(Date date, String pattern, String type) {
        String typeMin = "MIN", typeMax = "MAX";
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String dateStr = sdf.format(date);
        if (typeMin.equals(type)) {
            dateStr += " 00:00:00";
        } else if (typeMax.equals(type)) {
            dateStr += " 23:59:59";
        }
        return dateStr;
    }

    /**
     * 取一段时间的每一天
     */

    public static List<Date> findDates(String beginTime, String endTime) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dBegin = sdf.parse(beginTime);
        Date dEnd = sdf.parse(endTime);
        List dateList = new ArrayList();
        dateList.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            dateList.add(calBegin.getTime());
        }
        return dateList;
    }

    public static void main(String[] args) {
        try {
            List<Date> dates = findDates("2020-04-26", "2022-01-08");
            for (Date date : dates) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String daySourceStr = sdf.format(date);
                int dayInt = Integer.parseInt(daySourceStr.replace("-", ""));
                System.out.println(daySourceStr + "  ----  " + dayInt);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}
